package com.example.dqdq.lzyim_asm_tvapp;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dqdq.lzyim_asm_tvapp.api.API;
import com.example.dqdq.lzyim_asm_tvapp.https.HttpManger;
import com.example.dqdq.lzyim_asm_tvapp.utils.ActivityCollector;
import com.example.dqdq.lzyim_asm_tvapp.utils.FileDownLoad;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.x;

public class WarrantyCardActivity extends BaseActivity implements View.OnClickListener{

    private Button problemBtn;
    private Button sanBaoBtn;
    private Button serviceRecordBtn;
    private Intent intent ;

    private TextView khxm;

    private TextView sjh;

    private TextView sbxh;

    private TextView sfzh;

    private TextView gjsc;

    private TextView gjsj;

    private TextView gjjg;

    private TextView sppp;

    private TextView spxh;

    private TextView sptxm;

    private TextView ccrq;
    private TextView fwrq;

    private TextView fwlb;

    private TextView fwgcs;
    private TextView xxdz;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_warranty_card);
        initView();
        checkUpdate(WarrantyCardActivity.this);
        ActivityCollector.addActivity(this);
     }

    private void initView()
    {
        problemBtn= (Button) findViewById(R.id.problemBtn);
        serviceRecordBtn= (Button) findViewById(R.id.ServiceRecordBtn);
        sanBaoBtn= (Button) findViewById(R.id.SanBaoBtn);
        problemBtn.setOnClickListener(this);
        sanBaoBtn.setOnClickListener(this);
        serviceRecordBtn.setOnClickListener(this);
        khxm= (TextView) findViewById(R.id.khxm);
        sjh= (TextView) findViewById(R.id.sjh);
        sbxh= (TextView) findViewById(R.id.sbxh);
        gjsc= (TextView) findViewById(R.id.gjsc);
        gjsj= (TextView) findViewById(R.id.gjsj);
        gjjg= (TextView) findViewById(R.id.gjjg);
        sppp= (TextView) findViewById(R.id.sppp);
        spxh= (TextView) findViewById(R.id.spxh);
        sptxm= (TextView) findViewById(R.id.sptxm);
        ccrq= (TextView) findViewById(R.id.ccrq);
        fwrq= (TextView) findViewById(R.id.fwrq);
        fwlb= (TextView) findViewById(R.id.fwlb);
        fwgcs= (TextView) findViewById(R.id.fwgcs);
        sfzh= (TextView) findViewById(R.id.sfzh);
        xxdz= (TextView) findViewById(R.id.xxdz);
        SharedPreferences  preferences = getSharedPreferences("user", Context.MODE_PRIVATE);

        khxm.setText(preferences.getString("name", "暂无信息"));
        Log.e("test",preferences.getString("name", "暂无信息"));
        sjh.setText(preferences.getString("phone", "暂无信息"));
        sbxh.setText(preferences.getString("type", "暂无信息"));
        sfzh.setText(preferences.getString("identity", "暂无信息"));
        gjsc.setText(preferences.getString("purchase", "暂无信息"));
        gjsj.setText(preferences.getString("purchasedate", "暂无信息"));
        gjjg.setText(preferences.getString("purchaseprice", "暂无信息"));
        sppp.setText(preferences.getString("purchasebrand", "暂无信息"));
        spxh.setText(preferences.getString("purchasemodel", "暂无信息"));
        sptxm.setText(preferences.getString("purchasebarcode", "暂无信息"));
        ccrq.setText(preferences.getString("factorydate", "暂无信息"));
        fwrq.setText(preferences.getString("servicedate", "暂无信息"));
        fwlb.setText(preferences.getString("serviceclass", "暂无信息"));
        fwgcs.setText(preferences.getString("serviceengineer", "暂无信息"));
        xxdz.setText(preferences.getString("address", "暂无信息"));


    }


    private void checkUpdate(final Context context) {
        x.http().post(HttpManger.getParams(API.APK_UPDATE_URL, "", ""), new Callback.CommonCallback<String>() {

            @Override
            public void onSuccess(String result) {
                try {
                    PackageManager manager = context.getPackageManager();
                    PackageInfo info = null;
                    Log.e("test", result);
                    info = manager.getPackageInfo(context.getPackageName(), 0);
                    int version = info.versionCode;
                    JSONObject object = new JSONObject(result);
                     final JSONObject data=object.getJSONObject("data");
                    if (version < data.getInt("versioncode")) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    FileDownLoad fileDownLoad=new FileDownLoad();
                                    String path= null;
                                    path = fileDownLoad.downLoadApk(API.SERVERC_URL+data.getString("download"),context);
                                    Intent i = new Intent(Intent.ACTION_VIEW);
                                    i.setDataAndType(Uri.parse("file://" + path), "application/vnd.android.package-archive");
                                    context.startActivity(i);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        }).start();

                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId())
        {
            case R.id.problemBtn:
            {
                intent=new Intent(WarrantyCardActivity.this,problemActivity.class);
                startActivity(intent);

            }
            break;
            case R.id.ServiceRecordBtn:
            {
                intent=new Intent(WarrantyCardActivity.this,ServiceRecordActivity.class);
                startActivity(intent);
            }
            break;
            case R.id.SanBaoBtn:
            {
                intent=new Intent(WarrantyCardActivity.this,SanBaoActivity.class);
                startActivity(intent);
            }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }
}
